package com.letter.otools.day.diary;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.google.android.material.appbar.MaterialToolbar;
import com.letter.otools.day.AddItemActivity;
import com.letter.otools.day.Anniversary;
import com.letter.otools.day.AnniversaryActivity;
import com.letter.otools.day.BaseActivity;
import com.letter.otools.day.R;
import com.letter.otools.day.util.ImageUtils;
import com.letter.otools.day.util.PopupUtils;
import com.letter.otools.day.util.StringUtils;

import org.litepal.LitePal;

import java.util.Arrays;
import java.util.List;

public class DiaryDetailActivity extends BaseActivity {

    private int diaryId = -1;
    private MaterialToolbar toolbar;
    private TextView tvTitle;
    private TextView tvDate;
    private ImageView ivWeather;
    private ImageView ivMood;
    private TextView tvContent;
    private Diary diary;
    private LinearLayout imageLay;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        diaryId = getIntent().getIntExtra("diaryId", -1);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayout() {
        return R.layout.diary_detail;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.anniversary_toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        int itemId = item.getItemId();
        if (itemId == R.id.delete) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage("Confirm to delete this diary？");
            dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    LitePal.delete(Diary.class, diary.getId());
                    setResult(RESULT_OK);
                    Toast.makeText(getActivity(), "Removed!", Toast.LENGTH_SHORT).show();
                    finish();
                }
            });
            dialog.setNegativeButton("Cancel", (dialog1, which) -> {
            });
            dialog.show();
        } else if (itemId == R.id.edit) {
            intent = new Intent(this, DiaryAddActivity.class);
            intent.putExtra("diaryId", diaryId);
            resultLauncher.launch(intent);
        }
        return true;
    }

    @Override
    protected void onResultData(int resultCode, Intent intent) {
        if (resultCode == Activity.RESULT_OK) {
            //重新加载
            this.diary = LitePal.find(Diary.class, diaryId);
            setDiaryData();
            setResult(RESULT_OK);
        }
        super.onResultData(resultCode, intent);
    }

    @Override
    protected void initView() {
        //添加监听
        registerForActivityResult();
        setSupportToolbarBack(toolbar);
        diary = LitePal.find(Diary.class, diaryId);
        setDiaryData();
    }

    void setDiaryData() {
        tvTitle.setText(diary.getTitle());
        tvContent.setText(diary.getContent());
        tvDate.setText(TimeUtils.millis2String(diary.getCreateTime()));
        imageLay.setVisibility(StringUtils.isNotEmpty(diary.getImages()) ? View.VISIBLE : View.GONE);
        tvTitle.setVisibility(StringUtils.isNotEmpty(diary.getTitle()) ? View.VISIBLE : View.GONE);
        if (StringUtils.isNotEmpty(diary.getImages())) {
            String[] imageArray = diary.getImages().split(",");
            List<String> imageList = Arrays.asList(imageArray);
            for (int i = 0; i < imageList.size(); i++) {
                String s = imageList.get(i);
                ImageView imageView = new ImageView(this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.topMargin = SizeUtils.dp2px(10);
                imageView.setLayoutParams(layoutParams);
                imageView.setAdjustViewBounds(true);
                ImageUtils.showImage(this, s, imageView);

                final int fi = i;
                imageView.setOnClickListener(v -> {
                    PopupUtils.preViewImages(getActivity(), imageView, fi, imageList);
                });
                imageLay.addView(imageView);
            }
        }
    }


    @Override
    public void initBinder() {
        toolbar = findViewById(R.id.toolbar);
        tvTitle = findViewById(R.id.tvTitle);
        tvDate = findViewById(R.id.tvDate);
        ivWeather = findViewById(R.id.ivWeather);
        ivMood = findViewById(R.id.ivMood);
        tvContent = findViewById(R.id.tvContent);
        imageLay = findViewById(R.id.imageLay);
    }
}
